#include <stdio.h>
#include <math.h>

void compute_mean_3_()
{
    double value = 0.0f, mean_values, sum_values;
    int nb_values = 0;
    sum_values = 0.0f;
    char flag;

    printf("Input Y(continue) or N(stop)");
    scanf("%c", &flag);
    getchar();

    while(flag == 'Y' || flag == 'y')
    {
        printf("type the next value");
        scanf("%lf", &value);
        getchar();
        sum_values += value;
        nb_values = nb_values + 1;

        printf("Input Y(continue) or N(stop)");
        scanf("%c", &flag);
        getchar();
    }

    if(nb_values == 0)
        mean_values = 0.0f;
    else
        mean_values = sum_values / nb_values;
    printf("mean_values is  %lf", mean_values);
}


void practice_with_array()
{
    float array[100];

    for(int i = 0; i < 100; i ++)
    {
        array[i] = (float)i + 1;
    }

    for(int i = 0; i < 100; i ++)
    {
        if( (i+1)%3 == 0 && (i+1)%5 == 0 )
            array[i] *= 1;
        else if( (i+1)%3 == 0 )
            array[i] *= (-1);
        else if ((i+1)%5 == 0  )
            array[i] *= (-2);
    }

    for(int i = 0; i < 100; i ++)
    {
        printf("%f\n", array[i]);
    }
}


void horner_method_1()
{
    int N;
    printf("Input the degree N of the polynomial:\n");
    scanf("%d", &N);
    getchar();

    double a[N + 1];
    double p[N + 1];
    double sum = 0.0;
    double x;

    printf("Input x:\n");
    scanf("%lf", &x);

    for(int i = 0; i <= N; i ++)
    {
        printf("Input a[%d]", i);
        scanf("%lf", &a[i]);
        getchar();
    }

    p[0] = 1;
    for(int i = 1; i <= N; i ++)
    {
        p[i] = p[i - 1] * x;
    }

    for(int i = 0; i <= N; i ++)
    {
        sum = sum + a[i] * p[i];
    }

    printf("The result is %lf", sum);
}

void horner_method_2()
{
    int N;
    printf("Input the degree N of the polynomial:\n");
    scanf("%d", &N);
    getchar();

    double a[N + 1];
    double sum = 0.0;
    double x;

    printf("Input x:\n");
    scanf("%lf", &x);

    for(int i = 0; i <= N; i ++)
    {
        printf("Input a[%d]", i);
        scanf("%lf", &a[i]);
        getchar();
    }

    // Write your code here.
    for(int i = N; i >= 1; i --)
    {
        sum = (sum + a[i]) * x;
    }
    sum += a[0];

    printf("The result is %lf", sum);
}


void horner_method_3()
{
    int N;
    printf("Input the degree N of the polynomial:\n");
    scanf("%d", &N);
    getchar();

    double a[N + 1];
    double sum = 0.0;
    double x;

    printf("Input x:\n");
    scanf("%lf", &x);

    for(int i = 0; i <= N; i ++)
    {
        printf("Input a[%d]", i);
        scanf("%lf", &a[i]);
        getchar();
    }

    // Write your code here.
    for(int i = 0; i <=N-1; i++)
    {
        sum = sum + pow(x, i);
    }

    printf("The result is %lf", sum);
}

void calculate_pi_with_for_loop()
{
    double pi = 0;
    double n = 1;
    int sign = 1;

    for(int i = 0; i <= 100000; i ++)
    {
        pi += sign / n;
        sign *= -1;
        n += 2;
    }

    pi *= 4;
    printf("Pi is %lf", pi);
}


void calculate_pi_with_while_loop_1()
{
    double pi = 0;
    double n = 0;
    int sign = 1;

    // Change for loop to while loop
    // With termination condition: 1/(2n + 1) < 0.000001
    while(1/(2 * n + 1) >= 0.000001)
    {
        pi += sign / (2 * n + 1);
        sign *= -1;
        n += 1;
    }

    pi *= 4;
    printf("Pi is %lf\n", pi);

    printf("The calculation terminated at the %lfth term\n", n);
}

void calculate_pi_with_while_loop_2()
{
    double pi = 0;
    double n = 1;
    int sign = 1;

    // Change for loop to while loop
    // With termination condition: 1/(2n + 1) < 0.000001
    while( 1 / n >= 0.000001)
    {
        pi += sign / n;
        sign *= -1;
        n += 2;
    }

    pi *= 4;
    printf("Pi is %lf\n", pi);

    int m = ((int)n - 1) / 2;
    printf("The calculation terminated at the %dth term\n", m);
}


void calculate_exp()
{
    int N = 10000000;
    double x = 10.1, term, sum = 1.0;
    term = 1.0;
    for(int i = 1; i < N; i ++)
    {
        term = term * x / (double)i;
        sum += term;
    }
    printf("sum is %lf", sum);
}